/**
  ******************************************************************************
  * File Name          : envirctrl.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include <string.h>
#include "tskcfg.h"
#include "network.h"
#include "route.h"
#include "cJSON.h"
#include "dwlcd.h"

typedef struct
{
	cJSON *root;
  cJSON *params;
  cJSON *key;
  char  split_buf[64];
}route_lx_down_static_type;
/* 本文件中调用的一些变量,为了减少栈的消耗,定义到函数外的全局变量,使用前手动清0 */
static route_lx_down_static_type lx_down_static;

/* {"id":"2","version":"1.0","params":{"zsb":"LX60210310012","kg":"0"}} */
uint8_t Route_lx_down_act(const char *str, uint8_t len)
{
	uint8_t err = 0;
	uint8_t dev_index = 0;
  Route_PayloadType *proute = NULL;
  
  proute = pvPortMalloc(sizeof(Route_PayloadType));
  if(proute == NULL)
  {
    return 1;
  }
  
  memset(proute, 0, sizeof(Route_PayloadType));
  memset(&lx_down_static, 0, sizeof(route_lx_down_static_type));
  
/* *** */ 	
  lx_down_static.root = cJSON_Parse(str);
  
  lx_down_static.params = cJSON_GetObjectItem(lx_down_static.root, "params");
	if(lx_down_static.params == NULL)
  {
    err = 2;
    goto fnout;
  }
	

/* *** */  
	lx_down_static.key = cJSON_GetObjectItem(lx_down_static.params, "zsb");
	if((lx_down_static.key == NULL) || (lx_down_static.key->type != cJSON_String))
  {
    err = 3;
    goto fnout;
  }
  
	if(Route_IsDidMatchDevice(lx_down_static.key->valuestring, &dev_index) != 0)
	{
		err = 3;
    goto fnout;
	}
	
  
/************/
	lx_down_static.key = cJSON_GetObjectItem(lx_down_static.params, "kg");
	if((lx_down_static.key == NULL) || (lx_down_static.key->type != cJSON_String))
  {
    err = 4;
    goto fnout;
  }
	
  if(lx_down_static.key->valuestring[0] == '1')
  {
		((LtLx_ActType *)(proute->payload))->lxkg = 1;
  }
	else if(lx_down_static.key->valuestring[0] == '0')
	{
		((LtLx_ActType *)(proute->payload))->lxkg = 0;
	}
	else
	{
		err = 5;
		goto fnout;
	}
	
	proute->dev_addr  = idx2add(dev_index);
	proute->dev_type  = RT_DV_LX;
	proute->pl_type   = ROUTE_PLTYP_LX_DOACT;
	proute->trans_dir = ROUTE_TRSDIR_NET_TO_SUBDEV;
  proute->pl_len    = sizeof(LtLx_ActType);
  
	if(Route_EvewTo485_Qhd != NULL)
	{
		xQueueSend(Route_EvewTo485_Qhd, proute, 0);
	}
	
fnout:
  cJSON_Delete(lx_down_static.key);
  cJSON_Delete(lx_down_static.params);
  cJSON_Delete(lx_down_static.root);
	
  vPortFree(proute);
  
  return err;
}

uint8_t Route_lx_down_para(const char *str, uint8_t len)
{
	uint8_t err = 0;
	uint8_t dev_index = 0;
  int value;
  char *save = NULL;
  Route_PayloadType *proute = NULL;
  
  proute = pvPortMalloc(sizeof(Route_PayloadType));
  if(proute == NULL)
  {
    return 1;
  }
  
  memset(proute, 0, sizeof(Route_PayloadType));
  memset(&lx_down_static, 0, sizeof(route_lx_down_static_type));
  
/* *** */ 	
  lx_down_static.root = cJSON_Parse(str);
  
  lx_down_static.params = cJSON_GetObjectItem(lx_down_static.root, "params");
	if(lx_down_static.params == NULL)
  {
    err = 2;
    goto fnout;
  }
	

/* *** */  
	lx_down_static.key = cJSON_GetObjectItem(lx_down_static.params, "zsb");
	if((lx_down_static.key == NULL) || (lx_down_static.key->type != cJSON_String))
  {
    err = 3;
    goto fnout;
  }
  
	if(Route_IsDidMatchDevice(lx_down_static.key->valuestring, &dev_index) != 0)
	{
		err = 3;
    goto fnout;
	}
	
  
/************/
	lx_down_static.key = cJSON_GetObjectItem(lx_down_static.params, "lxot");
	if((lx_down_static.key == NULL) || (lx_down_static.key->type != cJSON_String))
  {
    err = 4;
    goto fnout;
  }
  
  value = atoi(lx_down_static.key->valuestring);
  if((value > 0) && (value < 59941))
  {
    ((LtLx_SyncType *)(proute->payload))->para.lxot = value;
  }
  else
	{
		err = 4;
		goto fnout;
	}
	
/************/
  lx_down_static.key = cJSON_GetObjectItem(lx_down_static.params, "lxtmen");
	if((lx_down_static.key == NULL) || (lx_down_static.key->type != cJSON_String))
  {
    err = 5;
    goto fnout;
  }
  
  if(lx_down_static.key->valuestring[0] == '0')
  {
    ((LtLx_SyncType *)(proute->payload))->para.lxtmen = 0;
  }
  else if(lx_down_static.key->valuestring[0] == '1')
  {
    ((LtLx_SyncType *)(proute->payload))->para.lxtmen = 1;
  }
  else
	{
		err = 5;
		goto fnout;
	}
  
/************/
  lx_down_static.key = cJSON_GetObjectItem(lx_down_static.params, "lxtm");
	if((lx_down_static.key == NULL) || (lx_down_static.key->type != cJSON_String))
  {
    err = 6;
    goto fnout;
  }
  
  if(strlen(lx_down_static.key->valuestring) > 63)
	{
		err = 6;
    goto fnout;
	}
	
	strncpy(lx_down_static.split_buf, lx_down_static.key->valuestring, 64);
	
	value = atoi(_strtok_r(lx_down_static.split_buf, "-", &save));
	if((value <0) || (value >86400))
	{
		err = 6;
		goto fnout;
	}
	
	((LtLx_SyncType *)(proute->payload))->para.lxtm[0] = value;
	
	for(uint8_t i=1; i<6; i++)
	{
		value = atoi(_strtok_r(NULL, "-", &save));
		if((value <0) || (value >86400))
		{
			err = 6;
			goto fnout;
		}
		
		((LtLx_SyncType *)(proute->payload))->para.lxtm[i] = value;
	}
  
  ((LtLx_SyncType *)(proute->payload))->hour   = Sys_Runtime.hour;
  ((LtLx_SyncType *)(proute->payload))->minute = Sys_Runtime.minute;
  ((LtLx_SyncType *)(proute->payload))->second = Sys_Runtime.second;
  
	proute->dev_addr  = idx2add(dev_index);
	proute->dev_type  = RT_DV_LX;
	proute->pl_type   = ROUTE_PLTYP_LX_PARA;
	proute->trans_dir = ROUTE_TRSDIR_NET_TO_SUBDEV;
  proute->pl_len    = sizeof(LtLx_SyncType);
  
	if(Route_EvewTo485_Qhd != NULL)
	{
		xQueueSend(Route_EvewTo485_Qhd, proute, 0);
	}
	
fnout:
  cJSON_Delete(lx_down_static.key);
  cJSON_Delete(lx_down_static.params);
  cJSON_Delete(lx_down_static.root);
	
  vPortFree(proute);
  
  return err;
}

uint8_t Route_lx_down_tb(const char *str, uint8_t len)
{
	uint8_t err = 0;
	uint8_t dev_index = 0;
  Route_PayloadType *proute = NULL;
  
  proute = pvPortMalloc(sizeof(Route_PayloadType));
  if(proute == NULL)
  {
    return 1;
  }
  
  memset(proute, 0, sizeof(Route_PayloadType));
  memset(&lx_down_static, 0, sizeof(route_lx_down_static_type));
  
/* *** */ 	
  lx_down_static.root = cJSON_Parse(str);
  
  lx_down_static.params = cJSON_GetObjectItem(lx_down_static.root, "params");
	if(lx_down_static.params == NULL)
  {
    err = 2;
    goto fnout;
  }
	

/* *** */  
	lx_down_static.key = cJSON_GetObjectItem(lx_down_static.params, "zsb");
	if((lx_down_static.key == NULL) || (lx_down_static.key->type != cJSON_String))
  {
    err = 3;
    goto fnout;
  }
  
	if(Route_IsDidMatchDevice(lx_down_static.key->valuestring, &dev_index) != 0)
	{
		err = 3;
    goto fnout;
	}
	
	proute->dev_addr  = idx2add(dev_index);
	proute->dev_type  = RT_DV_LX;
	proute->pl_type   = ROUTE_PLTYP_LX_SYNC;
	proute->trans_dir = ROUTE_TRSDIR_NET_TO_SUBDEV;
  proute->pl_len    = 0;
  
	if(Route_EvewTo485_Qhd != NULL)
	{
		xQueueSend(Route_EvewTo485_Qhd, proute, 0);
	}
	
fnout:
  cJSON_Delete(lx_down_static.key);
  cJSON_Delete(lx_down_static.params);
  cJSON_Delete(lx_down_static.root);
	
  vPortFree(proute);
  
  return err;
}
